package com.gree.scmservice.dao.saasDao;

import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasCommodity;
import org.nutz.dao.*;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.lang.util.NutMap;
import org.nutz.trans.Atom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 包路径: com.gree.scmservice.dao.saasDao
 * 项目名: scm-api
 * 创建时间: 2021-07-12 14:02
 * 描述: TODO
 **/
@Repository
public class SaasCommodityDao {

    @Autowired
    private Dao dao;

    /**
     * 新增
     * @param saasCommodity
     * @return
     */
    public SaasCommodity insertSaasCommodityDao(SaasCommodity saasCommodity){
        SaasCommodity insert = dao.insert(saasCommodity);
        return insert;
    }

    /**
     * 更新
     * @param saasCommodity
     * @return
     */
    public int updateSaasCommodityDao(SaasCommodity saasCommodity){
        final int[] a = {0};
        FieldFilter.locked(SaasCommodity.class,"^create_time$").run(new Atom() {
            @Override
            public void run() {
                a[0] = dao.update(saasCommodity);
            }
        });
        return a[0];
    }

    /**
     * 更新图片
     */
    public int updateSaasCommodityImage(String group_id, String image){
        return dao.update(SaasCommodity.class, Chain.make("image", image), Cnd.where("group_id","=",group_id));
    }

    /**
     * id查询
     * @param groupId
     * @return
     */
    public SaasCommodity getGroupIdCommodity(String groupId){
        SaasCommodity saasCommodity = dao.fetch(SaasCommodity.class, groupId);
        return saasCommodity;
    }

    /**
     * 分页查询全部
     * @return
     */
    public TablePageData<SaasCommodity> selectCommodityPage(SaasCommodity saasCommodity){
        Pager pager = dao.createPager(saasCommodity.getPageNum(), saasCommodity.getPageSize());
        saasCommodity.saasCommodityNoPage();
        Cnd cnd = Cnd.from(dao,saasCommodity);
        List query = dao.query(SaasCommodity.class, cnd, pager);
        pager.setRecordCount(dao.count(SaasCommodity.class, cnd));
        TablePageData<SaasCommodity> tablePageData = new TablePageData(query, pager);
        return tablePageData;
    }

    /**
     * 批量删除
     * @return
     */
    public int delCommodityIds(String[] ids) {
       int clear = dao.clear(SaasCommodity.class, Cnd.where("group_id", "in", ids));
       return clear;
    }
}
